<?php

namespace Admin\Controller;

class ManagerController extends AdminController {

    public function index() {
        $total = M('Manager')->count();
        $Page = new \Think\Page($total, 20);
        $result = M('Manager')->page($Page->nowPage, $Page->listRows)->order('id DESC')->select();
        foreach ($result as $key => $val) {
            $result[$key]['group_name'] = M('ManagerGroup')->where(['id' => $val['group']])->getField('name');
        }
        $this->assign('page', $Page->show());
        $this->assign('result', $result);
        $this->display();
    }

    public function add() {
        if (IS_POST) {
            $username = I('username', '');
            $password = I('password');
            $group = I('group', 0, 'intval');
            if (!$username) {
                $this->error('用户名不能为空');
            }
            if (!isUserName($username)) {
                $this->error('用户名格式不正确');
            }
            if (!$password) {
                $this->error('密码不能为空');
            }
            $len = strlen($password);
            if (!($len >= 6 && $len <= 16)) {
                $this->error('密码长度必须大于6位，小于16位');
            }
            $total = M('Manager')->where(['username' => $username])->count();
            if ($total) {
                $this->error('该用户名已经存在');
            }
            $password = entrypt_password($password);
            $data = [
                'username' => $username,
                'password' => $password,
                'add_time' => time(),
                'group' => $group,
            ];
            if (M('Manager')->data($data)->add()) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->assign('group_list', $this->groupList());
            $this->display();
        }
    }

    public function edit() {
        $id = I('id', 0, 'intval');
        if (IS_POST) {
            $password = I('password', '');
            $group = I('group');
            $data = [
                'id' => $id,
                'group' => $group,
            ];
            if ($password) {
                $len = strlen($password);
                if (!($len >= 6 && $len <= 16)) {
                    $this->error('密码长度必须大于6位，小于16位');
                }
                $password = entrypt_password($password);
                $data['password'] = $password;
            }
            if(M('Manager')->data($data)->save() !== false){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        } else {
            $result = M('Manager')->where(['id' => $id])->field('id,username,group')->find();
            $this->assign($result);
            $this->assign('group_list', $this->groupList());
            $this->display();
        }
    }

    public function delete($id) {
        $id = intval($id);
        if ($id == $this->admin_info['id']) {
            $this->error('不能删除自己');
        }
        M('Manager')->delete($id);
        $this->success('删除成功');
    }

    public function unique() {
        $username = I('username');
        $total = M('Manager')->where(['username' => $username])->count();
        if ($total) {
            exit(json_encode(['valid' => false]));
        } else {
            exit(json_encode(['valid' => true]));
        }
    }

    protected function groupList() {
        return M('ManagerGroup')->order('id ASC')->select();
    }

    public function group() {
        $total = M('ManagerGroup')->count();
        $Page = new \Think\Page($total, 20);
        $result = M('ManagerGroup')->order('id ASC')->page($Page->nowPage, $Page->listRows)->select();
        foreach ($result as $key => $val) {
            $result[$key]['total'] = M('Manager')->where(['group' => $val['id']])->count();
        }
        $this->assign('result', $result);
        $this->assign('page', $Page->show());
        $this->display();
    }

    public function group_add() {
        if (IS_POST) {
            $name = I('name', '');
            $supper = I('supper', 0, 'tinyintval');
            $purview = I('purview_id');
            if (!$name) {
                $this->error('用户组名称不能为空');
            }
            $total = M('ManagerGroup')->where(['name' => $name])->count();
            if ($total) {
                $this->error('用户组名称已经存在');
            }
            if ($supper) {
                $purview = [];
            } else {
                $purview = array2string($purview);
            }
            $data = [
                'name' => $name,
                'status' => 1,
                'supper' => $supper,
                'purview' => $purview,
            ];
            if (M('ManagerGroup')->data($data)->add()) {
                $this->success('添加成功', U('manager/group'));
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->assign('purview_list', $this->purviewList());
            $this->display();
        }
    }

    public function group_edit() {
        $id = I('id', 0, 'intval');
        if ($id == 1) {
            $this->error('系统用户组不允许编辑');
        }
        $result = M('ManagerGroup')->find($id);
        if (!$result) {
            $this->error('用户组不存在');
        }
        if (IS_POST) {
            $name = I('name', '');
            $supper = I('supper', 0, 'tinyintval');
            $purview = I('purview_id');
            if (!$name) {
                $this->error('用户组名称不能为空');
            }
            $total = M('ManagerGroup')->where(['name' => $name, 'id' => ['neq', $id]])->count();
            if ($total) {
                $this->error('用户组名称已经存在');
            }
            if ($supper) {
                $purview = [];
            } else {
                $purview = array2string($purview);
            }
            $data = [
                'id' => $id,
                'name' => $name,
                'status' => 1,
                'supper' => $supper,
                'purview' => $purview,
            ];
            if (M('ManagerGroup')->data($data)->save() !== false) {
                $this->success('编辑成功', U('manager/group'));
            } else {
                $this->error('编辑失败');
            }
        } else {

            $result['purview'] = string2array($result['purview']);
            $this->assign($result);
            $this->assign('purview_list', $this->purviewList());
            $this->display();
        }
    }

    public function group_delete($id) {
        $id = intval($id);
        if (!$id) {
            $this->error('参数错误');
        }
        if ($id == 1) {
            $this->error('系统用户组无法删除');
        }
        $total = M('Manager')->where(['group' => $id])->count();
        if ($total) {
            $this->error('该用户组下包含用户，无法删除');
        }
        M('ManagerGroup')->delete($id);
        $this->success('删除成功');
    }

    public function group_unique() {
        $name = I('name');
        $id = I('id', 0, 'intval');
        $where = ['name' => $name];
        if ($id) {
            $where['id'] = ['neq', $id];
        }
        $total = M('ManagerGroup')->where($where)->count();
        if ($total) {
            exit(json_encode(['valid' => false]));
        } else {
            exit(json_encode(['valid' => true]));
        }
    }

    public function purviewList() {
        $modules = M('PurviewModule')->order('sort ASC,id ASC')->select();
        $modules = tree_array($modules);
        $actions = M('Purview')->select();
        $action_list = [];
        foreach ($actions as $action) {
            $action_list[$action['mid']][] = $action;
        }
        foreach ($modules as &$module) {
            foreach ($module['child'] as &$cmodule) {
                $cmodule['action_list'] = $action_list[$cmodule['id']];
            }
        }
        return $modules;
    }

}
